<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

class BookController Extends BaseController
{
	private $page = 1;
	private $numPageShow = '';
	
  public function index($page = 1) {  		  	            	   
  	  
  	//echo "page = ".$page;
  	$this->page = $page; 	

  	//get book number of one special page
  	$num = $this->registry->db->get('configManager')->getById(2)->get("value");
    $showedBooks = $this->registry->db->get('bookManager')->getAllCategoryBooks(($this->page-1)*$num, $num);
    $numOfBook =  $this->registry->db->get('bookManager')->getAllBooks();
    $catBooksContent = '<div class="title">
    						<span class="title_icon">
    						<img src="{bstoViewPath}images/bullet1.gif" alt="" title="" />
    						</span>All books</div>
           				<div class="new_products">';          	 	    
//  	$catBooksContent = $catBooksContent . $this->registry->template->getCategoryBooksContent($showedBooks, $this->showPagingBar($numOfBook, $num))
//  						.'</div>';		  	
    $catBooksContent .=  '</div>';
    $this->registry->template->setMenuKey('category'); 
  	$tags = array('bstoHead'=>"");
    	$user = $this->registry->db->get('userManager')->getCurrentUser();
        if($user != null){
        	$tags['login'] = '<li id="login"><span> Hello, '. $user->get('username').'</span><a href="{bstoRootPath}logout">Logout</a></li>';	
        }else{
        	$tags['login'] = '<li id="login"><a href="{bstoRootPath}login">Login</a></li>';
        }   
    $output = $this->registry->template->getPageContent('index', 'Books', $catBooksContent, $tags);
    				                      
    echo $output;      
  }   
  
   public function showPagingBar( $numOfBook, $num){
        $numOfPage = ceil($numOfBook / $num);
        $str = '';
        if($numOfBook == 0 || $numOfPage ==1 ){
            $str .= '<div class="pagination"> </div>';
        }else {
            if($numOfPage == 1){
                $str ='<div class="pagination"> <span class="disabled">[First]</span> ';
                $str .='<span class="disabled">&lt;&lt;</span> ';
                $str .= '<a href="{bstoRootPath}book/1"><span class="current">1</span></a>';
                $str .= '<span class="disabled"> &gt;&gt; </span>';
                $str .= '<span class="disabled"> [Last] </span>';
                $str .= '</div>';
            }else if ($this->page == 1){
                $str ='<div class="pagination">' ;
                $i = 0;
                for($i=$this->page ; $i < $this->page + 3; $i++){
                    if($i <= $numOfPage){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}book/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}book/' .$i. '">' .$i. '</a>';    
                        }
                            
                    }            
                }
                if($i < $numOfPage){
                    $str .= "...";
                }
                $next = $this->page + 1;
                $str .= '<a href="{bstoRootPath}book/' .$next . '"> &gt;&gt; </a>';
                $str .= '<a href="{bstoRootPath}book/' .$numOfPage . '"> [Last] </a>';
                $str .= '</div>';
        		            
            } else if ($this->page == $numOfPage){
                $previous = $this->page - 1;
                $str ='<div class="pagination"> <a href="{bstoRootPath}book/1">[First]</a>';
                $str .='<a href="{bstoRootPath}book/' .$previous.'">&lt;&lt;</a>';
                $i = $this->page - 2;
                if($i > 1){
                    $str .= "...";
                }
                for($i=$this->page - 2; $i <= $numOfPage; $i++){
                    if($i >= 1 && $i <= $numOfPage){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}book/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}book/' .$i. '">' .$i. '</a>';    
                        }                    
                    }            
                }    
               
                $str .= '</div>';
                       
            } else {
                $previous = $this->page - 1;
                $next = $this->page + 1;
                $str .='<div class="pagination"> <a href="{bstoRootPath}book/1">[First]</a>';
                $str .='<a href="{bstoRootPath}book/' .$previous. '">&lt;&lt;</a>';
                $i = $this->page;
                for($i = $this->page - 1 ; $i < $this->page + 2; $i++){
                    if($i <= $numOfPage){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}book/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}book/' .$i. '">' .$i. '</a>';    
                        }    
                    }
                        
                }
                if($i < $numOfPage){
                    $str .= "...";
                }
                $str .= '<a href="{bstoRootPath}book/' .$next. '"> &gt;&gt; </a>';
                $str .= '<a href="{bstoRootPath}book/' .$numOfPage . '"> [Last] </a>';
                $str .= '</div>'; 
                     
            }
        }
        
        return $str;   
   }
}
?>